<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class ProfileController extends Controller
{
    public function pwd()
    {
        $admin = Auth::user();
        return view('admin.profile.pwd')->with('admin', $admin);
    }

    /**
     * 更新密码
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = \Validator::make($request->all(), [
            'password' => 'required|confirmed',
            'password_confirmation' => 'required|same:password',
        ],[
            'password.required' => '密码不能空',
            'new_password.required' => '确定密码不能空',
            'password.confirmed'=>'两次输入密码不一致'
        ]);

        if ($validator->fails()) {
            return redirect()->to(route('admin.profile.pwd'))->withErrors($validator->errors()->first());
        }
        $pwd = $request->input("password");
        $pwd = Hash::make($pwd);
        $id = Auth::id();
        $res = Admin::where("id",$id)->update(['password' => $pwd,'updated_at' => date("Y-m-d H:i:s")]);
        if($res){
            return redirect()->to(route('admin.profile.pwd'))->with(['status' => '设置成功']);
        }
        return redirect()->to(route('admin.profile.pwd'))->withErrors($res['msg']);
    }
}
